package com.hulk.ratel.web.filter;


import com.hulk.ratel.common.constant.enums.BussErrCode;
import com.hulk.ratel.common.exception.BaseRteException;
import com.hulk.ratel.web.exceptionhandler.GlobalExceptionHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.web.filter.OncePerRequestFilter;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebFilter;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


/**
 *  过滤器
 *
 * @author hulk
 */
@Slf4j
@Component
@Order(Ordered.HIGHEST_PRECEDENCE)
@WebFilter(filterName = "globalFilter", urlPatterns = "/*",initParams={@WebInitParam(name ="EXCLUDED_PAGES" , value = "/rest/heartbeat;/favicon.ico")})
public class GlobalOncePerRequestFilter extends OncePerRequestFilter {



    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {

        try {
            filterChain.doFilter(request, response);
        } catch (BaseRteException e) {
            GlobalExceptionHandler.processException(request, response, e.getErrorCode(), e.getMessage(), e);
        } catch (Exception e) {
            GlobalExceptionHandler.processException(request, response, BussErrCode.E_999999.getErrCode(), BussErrCode.E_999999.getErrDesc(), e);
        }
    }



}
